clear

*Get the full roster of four-year universities in each year from IPEDS, since so few two-year colleges respond to TFS, 
*and then merge with data denoting whether there were any TFS respondents

use "degenrollempstaff_inst_8018.dta"

keep if four_yr==1

keep unitid year instnm cty_fips statefips total_BAdeg normalschool_schoolfile

rename normalschool_schoolfile prevnormsch

*Merge the IPEDS roster with data set denoting normal school or asylum counties
merge m:1 cty_fips using "justnormasylum.dta"
rename _merge mergeinstnorm

*Keep only the universities that have a county fips and are in a normal school or asylum county
keep if mergeinstnorm==3
save college_inst_8018_justbasic.dta, replace

clear
use "1 DEMOGRAPHICS.dta"
keep if YEAR>=1982
tolower

*Merge with acerecode year unitid dataset

merge m:1 acerecode year using TFS_unitid_aceyear.dta
rename _merge mergedemoaceyear

gen nonmissingstudwgt = studwgt~=0 & studwgt~=.
tab nonmissingstudwgt mergedemoaceyear

drop if mergedemoaceyear==2

bysort unitid year: egen anynonmissingstudwgtx = total(nonmissingstudwgt)
gen anynonmissingstudwgt = anynonmissingstudwgtx >=1

keep unitid year anynonmissingstudwgt 
egen tagunitidyr = tag(unitid year)
keep if tagunitidyr==1

merge 1:1 unitid year using college_inst_8018_justbasic.dta
rename _merge mergeroster

tab mergeroster if anynonmissingstudwgt==1

keep if hasnormalorasylum==1

*The above command will drop universities in TFS data that do not merge to the IPEDS roster
*(service academies) because hasnormalorasylum should be equal to missing for these observations

tab mergeroster

keep if year<=2010

count

gen tfsresponse = mergeroster==3
tab tfsresponse

save TFS_roster_NA.dta, replace

*Merge with distance education to focus on non-distance education places

clear
insheet using disted.csv
rename ef2018a_distallstudentsenrolled totenroll18
rename ef2018a_diststudentsenrolledexcl excldist18

gen pctenralldist = excldist18/totenroll18

keep unitid pctenralldist

merge 1:m unitid using TFS_roster_NA.dta
rename _merge mergedist


drop if mergedist==1

su pctenralldist, d

gen instnmlower = strlower(instnm)

gen univph = strmatch(instnmlower, "university of phoenix*")
gen univstr = strmatch(instnmlower, "strayer university*")
gen univkap = strmatch(instnmlower, "kaplan university*")

save  TFS_roster_NA_disted.dta, replace

gen tfsresponseposweight = 1 if tfsresponse==1 & anynonmissingstudwgt==1
replace tfsresponseposweight = 0 if tfsresponse==0|anynonmissingstudwgt==0

*Appendix E1 Footnote 69: Mean response rates and number of counties and universities
su tfsresponseposweight if hasnormalschool==1 & year>=1982 
su tfsresponseposweight if hasnormalschool==0 & hasnormalorasylum==1 & year>=1982

su tfsresponseposweight if hasnormalschool==1 & year>=1982 [aw=total_BAdeg]
su tfsresponseposweight if hasnormalschool==0 & hasnormalorasylum==1 & year>=1982 [aw=total_BAdeg]

distinct unitid if hasnormalschool==1 & tfsresponseposweight==1 & year>=1982
distinct cty_fips if hasnormalschool==1 & tfsresponseposweight==1 & year>=1982

distinct unitid if hasnormalschool==0 & tfsresponseposweight==1 & year>=1982 
distinct cty_fips if hasnormalschool==0 & tfsresponseposweight==1 & year>=1982

distinct unitid if hasnormalschool==1 & tfsresponse==1 & year>=1982
distinct cty_fips if hasnormalschool==1 & tfsresponse==1 & year>=1982

distinct unitid if hasnormalschool==0 & tfsresponse==1 & year>=1982
distinct cty_fips if hasnormalschool==0 & tfsresponse==1 & year>=1982

*Table A45, Column 1
reghdfe tfsresponseposweight hasnormalschool if year>=1982 [aw=total_BAdeg], absorb(statefips#year) cluster(cty_fips)
outreg2 using univresponse.xls, replace excel dec(3)
gen samp_partic = e(sample)==1
su tfsresponse if hasnormalschool==1 & e(sample)==1 [aw=total_BAdeg]
su tfsresponse if hasnormalschool==0 & e(sample)==1 [aw=total_BAdeg]

save "TFS_roster_NA.dta", replace

gen online = pctenralldist>=.5
replace online = 0 if pctenralldist==.

*Table A45, Column 2
reghdfe tfsresponseposweight hasnormalschool if online==0 & year>=1982 [aw=total_BAdeg], absorb(statefips#year) cluster(cty_fips)
outreg2 using univresponse.xls, append excel dec(3)

tab mergedist if e(sample)==1

save "TFS_roster_NA.dta", replace

